/*
 * 		Test.java
 *   Copyright (C) 2015  Adrián E. Córdoba [software.asia@gmail.com]
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package ar.com.adriancordoba.app.console.atps.configurations;

import org.apache.commons.configuration.CompositeConfiguration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.XMLConfiguration;
import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
 *
 * @author Adrián E. Córdoba
 */
public class Configuration {
	public static final String CONFIGURATION_PATH = "conf" + System.getProperty("file.separator");
	private static final Logger logger = LogManager.getLogger(Configuration.class);
	private static CompositeConfiguration configuration;
	
	private Configuration(){
		configuration= new CompositeConfiguration();
	}

	public static CompositeConfiguration getInstance(String filename) throws ConfigurationException {
		if (configuration == null) {
			new Configuration();
		}
		XMLConfiguration xmlConfiguration = null;
		try {
			xmlConfiguration = new XMLConfiguration();
			xmlConfiguration.setDelimiterParsingDisabled(true);
			xmlConfiguration.load(CONFIGURATION_PATH + filename);
		} catch (ConfigurationException exception) {
			logger.fatal("Cannot read configuration file.");
			throw exception;
		}
		xmlConfiguration.setReloadingStrategy(new FileChangedReloadingStrategy());
		configuration.addConfiguration(xmlConfiguration);

		return configuration;
	}
}
